package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.PictureListItemVO;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.PictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper mapper;
    @Autowired
    private AlbumMapper albumMapper;


    @Override
    public void addNew(PictureAddNewParam pictureAddNewParam) {
        log.info("开始:【新增图片】");
        log.debug("传入参数,pictureAddNewParam={}", pictureAddNewParam);
        //检查图片是否存在
        AlbumStandardVO standardById = albumMapper.getStandardById(pictureAddNewParam.getAlbumId());
        log.info("业务：根据图片ID检索数据库,standardById={}", standardById);
        if(standardById==null){
            log.info("结果2:根据图片id查询，结果为null，抛出异常...");
            String message = "新增图片失败，图片不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewParam,picture);
        picture.setIsCover(0)
                .setGmtCreate(LocalDateTime.now()).setGmtModified(LocalDateTime.now());

        log.debug("新增图片数据,picture={}", picture);
        int rows = mapper.insert(picture);
        if(rows != 1){
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            String message = "添加图片失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增图片结果,rows={}", rows);
    }

    @Override
    public void deleteById(Long id) {
        log.info("开始:【删除图片】");
        log.debug("传入参数,id={}", id);

        //检查图片ID检查图片是否存在数据，如果不存在，则抛出异常
        PictureStandardVO pictureStandardVO = mapper.getStandardById(id);
        log.info("规则：根据图片id检索数据库结果,pictureStandardVO={}", pictureStandardVO);
        if(pictureStandardVO == null){
            String message = "删除图片失败，图片数据不存在！";
            log.info("结果2:根据图片id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = mapper.deleteById(id);
        if(rows != 1){
            String message = "删除图片失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.info("结果1:删除图片结果,rows={}", rows);
    }

    @Override
    public PictureStandardVO getStandardById(Long id) {
        log.info("开始:【根据ID查询图片】");
        log.debug("传入参数,id={}", id);
        PictureStandardVO standardById = mapper.getStandardById(id);

        //根据图片ID检查图片是否存在数据，如果不存在，则抛出异常
        if(standardById==null){
            log.info("结果2:根据图片id查询详情，结果为null，抛出异常...");
            String message = "查询图片详情失败，图片数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.info("结果1:查询出数据,standardById");
        return standardById;
    }

    @Override
    public PageData<PictureListItemVO> listByAlbumId(Long albumId, Integer pageNum) {
        Integer pageSize = 5;
        return listByAlbumId(albumId, pageNum, pageSize);
    }

    @Override
    public PageData<PictureListItemVO> listByAlbumId(Long albumId, Integer pageNum, Integer pageSize) {
        log.info("开始:【查询-图片-分页】");
        log.debug("传入参数, albumId={}, pageNum={}, pageSize={}", albumId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<PictureListItemVO> list = mapper.listByAlbumId(albumId);
        PageInfo<PictureListItemVO> pageInfo = new PageInfo<>(list);
        PageData<PictureListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }
}
